import BadgeManage from "../../core/badge/BadgeManage";
import { ContextEvent, ContextType } from "../../core/common/GameCost";
import DataManage from "../../core/db/DataManage";
import LocalDb from "../../core/db/LocalStorage";
import EventManage from "../../core/event/EventManage";
import { UIBase } from "../../core/gui/UIBase";
import { Fview, UILayer } from "../../core/gui/UIDefine";
import UIManage from "../../core/gui/UIManage";
import SoundManage from "../../core/sound/SoundManage";
import TimeManage from "../../core/time/TimerManage";
import Utils from "../../core/utils/Utils";
import NetMask from "../../moduleview/common/NetMask";
import MainDlg from "../../moduleview/main/Maindlg";
import Loading from "../../moduleview/login/Loading";
import PlayerModule from "../PlayerModule";
import EventType from "./EventType";
import { ResEnum } from "./ResEnum";
import MainUI from "../../moduleview/main/MainUI";
import Context from "../../core/context/Context";
import BadgeUI from "../../moduleview/main/BadgeUI";
import game from "../../core/game";
import Guide from "../../core/guide/Guide";
import { SpineManage } from "../../core/anim/SpineManage";
import Net from "../../core/net/Net";
import GuideManage from "../../core/guide/GuideManage";
import ResManage from "../../core/resources/ResManage";
import { PLATFORM } from "../../core/platform/Platform";

export default class ContextModel {

    constructor() {

        this.initMamage();
        this.initModel();
        this.initUI();

        this.onAfterLogic();

        Context.Instance.runEvent(ContextType.Manage, ContextEvent.initialize)
    }

    //游戏基本模块
    private initMamage() {
        this.registManage(EventManage);
        this.registManage(BadgeManage);
        this.registManage(LocalDb);
        this.registManage(DataManage);
        this.registManage(GuideManage);
        this.registManage(UIManage);
        this.registManage(Net);
        this.registManage(SoundManage);
        this.registManage(TimeManage);
        this.registManage(SpineManage);
        this.registManage(ResManage);
    }

    //注册游戏核心逻辑脚本
    private initModel() {

        this.regiseModel(PlayerModule);
    }

    //注册UI
    private initUI() {
        this.registerUI(ResEnum.login, Loading);
        this.registerUI(ResEnum.common, NetMask);
        this.registerUI(ResEnum.main, MainDlg);
        this.registerUI(ResEnum.main, MainUI);
        this.registerUI(ResEnum.main, BadgeUI);
        this.registerUI(ResEnum.guide, Guide);
    }

    //一些需要在脚本注册时的逻辑
    private onAfterLogic() {
        let context = Context.Instance;

        game.evm = context.get(EventManage);
        game.ui = context.get(UIManage);
        game.badge = context.get(BadgeManage);
        game.db = context.get(LocalDb);
        game.data = context.get(DataManage);
        game.guide = context.get(GuideManage);
        game.net = context.get(Net);
        game.sound = context.get(SoundManage);
        game.time = context.get(TimeManage);
        game.spine = context.get(SpineManage);
        game.app = PLATFORM;

        context.get(SpineManage).defaultParent = context.get(UIManage).getLayer(UILayer.eff);

    }

    private registerUI<T extends Fview>(pkg: string, ui: new () => UIBase<T>) {
        let key = Utils.getGUIDByObject(ui);
        let context = Context.Instance;
        context.get(UIManage).regUI(pkg, key)
        context.register(ui, ContextType.UI);
    }

    //注册时实列
    private registManage(model: new () => any) {
        let context = Context.Instance;
        context.register(model, ContextType.Manage);
    }

    //使用时实列
    private regiseModel(model: new () => any) {
        let context = Context.Instance;
        context.register(model, ContextType.Moudule)
    }

    /**游戏循环 1s*/
    updateSecond() {
        let context = Context.Instance;
        context.runEvent(ContextType.Manage, ContextEvent.GameUpdate);
        context.runEvent(ContextType.Moudule, ContextEvent.GameUpdate);
        context.get(EventManage).event(EventType.GameLoop)
    }

    /**游戏循环，每帧循环 */
    updateLoop() {
        let context = Context.Instance;
        context.runEvent(ContextType.Manage, ContextEvent.GameUpdateFrame);
        context.runEvent(ContextType.Moudule, ContextEvent.GameUpdateFrame);
        context.get(EventManage).event(EventType.GameLoopFrame);
    }
}